package com.hxf.web.action.menu;

import java.io.IOException; 
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List; 
 
import org.apache.struts2.convention.annotation.Result;
import org.apache.struts2.convention.annotation.Results;

import com.hxf.common.DateUtil;
import com.hxf.common.GlobalMPURL;
import com.hxf.dto.AdminRoleDto;
import com.hxf.dto.AdminUserDto;
import com.hxf.dto.MenuDto;
import com.hxf.dto.ReturnData;
import com.hxf.exception.ServiceException; 
import com.hxf.web.action.WebActionBase;
import com.hxf.web.vo.MenuVo;


/**
 * 菜单管理Action
 * @author tengyicheng
 * @version [1.0, 2013/09/22]
 * @see [相关类/方法]
 * @since [产品/模块版本]
 */
@Results({@Result(name = "success", location = "../common/success.jsp"), @Result(name = "error", location = "../common/error.jsp"), @Result(name = "msg", location = "../common/msg.jsp")})
public class MenuAction extends WebActionBase {
	private static final long serialVersionUID = 1L;
	
	private Integer menuId; // 主键菜单id
	private String menuName; // 菜单名称
	private Integer menuOrder; // 排序号
	private Integer parentId; // 上级菜单ID
	private String menuUrl; // 菜单跳转地址
	private Integer status; // 状态
	
	private Integer currentMenuId;
	private Integer currentId;
	
	private List<MenuDto> menuList;
	
	/**
	 * 查询菜单信息
	 * @return
	 */
	public String menuList() {
		try {
			//菜单权限验证
			if (!menuPrivilegeValidate(GlobalMPURL.MENU_PRIVILEGE_URL)) {
				return "msg";
			}
			MenuVo menu = new MenuVo();//Vo 实体类
			menu.setMenuName(menuName);
			//获取该管理员级别
			//UserDto user = (UserDto)getSession().getAttribute("userDto");
			AdminUserDto adminUser = (AdminUserDto)getSession().getAttribute("adminUserDto");
			AdminRoleDto roleDto = super.serviceFacade.getUserService().selectUserRole(adminUser.getId());
			menu.setRoleId(roleDto.getRoleId());
			
			ReturnData<MenuDto> menuData = super.serviceFacade.getMenuService().findByMenu(menu, targetPage, pageSize);
			
			menuList = menuData.getResultlist();
			
			setRecordCount(menuData.getRecordcount());
			
		} catch (ServiceException e) {
			logger.debug(e.getMessage(), e);
		}
		return "menu-list";
	}

	/**
	 * 修改菜单信息
	 * @return
	 */
	public String menuUpdate() {
		try {
			//菜单权限验证
			if (!menuPrivilegeValidate(GlobalMPURL.MENU_PRIVILEGE_URL)) {
				return "msg";
			}
			MenuDto menu = (MenuDto) super.serviceFacade.getMenuService().get(MenuDto.class, menuId);
			this.currentMenuId = menu.getParentId();
			this.currentId = menu.getMenuId();
			//查询上级菜单
			menuList = super.serviceFacade.getMenuService().findByMenu(null);
			this.setValues(menu);
		} catch (ServiceException e) { 
			logger.debug(e.getMessage(), e);
		}
		return "menu-update";
	}
	
	/**
	 * 跳转菜单页面 
	 * @return
	 */
	public String menuAdd() {
		try {
			//菜单权限验证
			if (!menuPrivilegeValidate(GlobalMPURL.MENU_PRIVILEGE_URL)) {
				return "msg";
			}
			menuList = super.serviceFacade.getMenuService().findByMenu(null);
		} catch (ServiceException e) {
			logger.debug(e.getMessage(), e);
		}
		return "menu-add";
	}
	
	/**
	 * 添加或者修改菜单信息
	 * @return
	 */
	public String menuSaveOrUpdate() { 
		try {
			//菜单权限验证
			if (!menuPrivilegeValidate(GlobalMPURL.MENU_PRIVILEGE_URL)) {
				return "msg";
			} 

			MenuDto menu = new MenuDto();
			menu.setMenuId(menuId);		// 主键菜单id
			menu.setMenuName(menuName);// 菜单名称
			menu.setMenuOrder(menuOrder);// 排序号
			menu.setParentId(parentId);// 上级菜单ID
			menu.setMenuUrl(menuUrl);// 菜单跳转地址
			menu.setStatus(status);	// 状态
			menu.setModifyDate(getAdminUser().getUserName());// 修改菜单用户
			menu.setModifyDate(DateUtil.getCurrentDate(DateUtil.DATE_PATTERN_M));//修改时间
			
			if (menuId == null) {
				menu.setCreateTime(DateUtil.getCurrentDate(DateUtil.DATE_PATTERN_M));//创建时间
				menu.setCreateUser(getAdminUser().getUserName());//创建用户名
				super.serviceFacade.getMenuService().save(menu);//保存
			} else { 
				super.serviceFacade.getMenuService().updateMenu(menu);//修改			
			}
			requestURL = "menu!menuList.html";
		} catch (ServiceException e) {
			e.printStackTrace();
			logger.debug(e.getMessage(), e);
		}
		return SUCCESS;
	} 
	
	/**
	 * 删除菜单信息
	 * @return
	 */
	public String menuDelete() {
		try {
			//菜单权限验证
			if (!menuPrivilegeValidate(GlobalMPURL.MENU_PRIVILEGE_URL)) {
				return "msg";
			}
			MenuDto menu = new MenuDto();
			menu.setMenuId(menuId);
			super.serviceFacade.getMenuService().delete(menu);
			super.println("success");
		} catch (ServiceException e) {
			logger.debug(e.getMessage(), e);
		} catch (IOException e) {
			logger.debug(e.getMessage(), e);
		}
		return null;
	}

	private void setValues(MenuDto menu) { 
		this.menuId = menu.getMenuId();
		this.menuName = menu.getMenuName();
		this.menuOrder = menu.getMenuOrder();
		this.parentId = menu.getParentId(); 
		this.menuUrl = menu.getMenuUrl(); 
		this.status = menu.getStatus();
	}
	
	public List<MenuDto> getMenuList() {
		return menuList;
	}

	public void setMenuList(List<MenuDto> menuList) {
		this.menuList = menuList;
	}
	 
	public Integer getMenuId() {
		return menuId;
	}

	public void setMenuId(Integer menuId) {
		this.menuId = menuId;
	}

	public String getMenuName() {
		return menuName;
	}

	public void setMenuName(String menuName) {
		this.menuName = menuName;
	}

	public Integer getMenuOrder() {
		return menuOrder;
	}

	public void setMenuOrder(Integer menuOrder) {
		this.menuOrder = menuOrder;
	}

	public Integer getParentId() {
		return parentId;
	}

	public void setParentId(Integer parentId) {
		this.parentId = parentId;
	}

	 
	public String getMenuUrl() {
		return menuUrl;
	}

	public void setMenuUrl(String menuUrl) {
		this.menuUrl = menuUrl;
	}
 

	public Integer getStatus() {
		return status;
	}

	public void setStatus(Integer status) {
		this.status = status;
	}

	public Integer getCurrentMenuId() {
		return currentMenuId;
	}

	public void setCurrentMenuId(Integer currentMenuId) {
		this.currentMenuId = currentMenuId;
	}

	public Integer getCurrentId() {
		return currentId;
	}

	public void setCurrentId(Integer currentId) {
		this.currentId = currentId;
	}
	 
}
